from datetime import datetime

from DjangoUeditor.models import UEditorField
from django.db import models


# Create your models here.
class GoodsCategory(models.Model):
    """商品分类"""
    CATEGORY_TYPE = (
        (1, "一级类目"),
        (2, "二级类目"),
        (3, "三级类目"),
    )

    name = models.CharField('类别名', default="", max_length=30, help_text="类别名")
    code = models.CharField("类别code", default="", max_length=30, help_text="类别code")
    desc = models.TextField("类别描述", default="", help_text="类别描述")
    # 目录树级别
    category_type = models.IntegerField("类目级别", choices=CATEGORY_TYPE, help_text="类目级别")
    # 一级分类: 电器    二级分类: 微波炉、电磁炉...
    # 一级分类：二级分类 = 1:N
    # 设置models有一个指向自己的外键
    # related_name="sub_cat"反向引用， 一级分类可以通过sub_cat属性获取该分类的子分类信息。
    parent_category = models.ForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, verbose_name="父类目级别",
                                        help_text="父目录", related_name="sub_cat")
    is_tab = models.BooleanField("是否导航", default=False, help_text="是否导航")
    add_time = models.DateTimeField("添加时间", default=datetime.utcnow, help_text='添加时间')

    class Meta:
        verbose_name = "商品类别"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Goods(models.Model):
    """产品"""
    goods_sn = models.CharField("商品唯一货号", max_length=50, default="", help_text='商品唯一货号')
    name = models.CharField("商品名", max_length=100, help_text='商品名')
    click_num = models.IntegerField("点击数", default=0, help_text='点击数')
    # 销售量变化:
    #       1. 增加订单并支付成功:
    sold_num = models.IntegerField("商品销售量", default=0, help_text='销售量')
    fav_num = models.IntegerField("收藏数", default=0)
    # 库存数变化：
    #       1. 购物车添加商品信息: 库存量-购物车数量
    #       2. 购物车更新商品数量:  - 10 +
    #       3. 删除购物车的商品:   库存量增加
    goods_num = models.IntegerField("库存数", default=0)
    market_price = models.FloatField("市场价格", default=0)
    shop_price = models.FloatField("本店价格", default=0, help_text='本店价格')
    goods_brief = models.TextField("商品简短描述", max_length=500)
    # MEDIA_ROOT = os.path.join(BASE_DIR, "media") ===== media/goods/images/
    # 导入from DjangoUeditor.models import UEditorField
    goods_desc = UEditorField(verbose_name=u"内容", imagePath="goods/images/", width=1000, height=300,
                              filePath="goods/files/", default='')
    ship_free = models.BooleanField("是否承担运费", default=True)
    # 首页中展示的商品封面图
    goods_front_image = models.ImageField(upload_to="goods/images/", null=True, blank=True, verbose_name="封面图")
    # 首页中新品展示
    is_new = models.BooleanField("是否新品", default=False)
    # 商品详情页的热卖商品，自行设置
    is_hot = models.BooleanField("是否热销", default=False)
    add_time = models.DateTimeField("添加时间", default=datetime.utcnow)

    # 商品分类:商品: 1:N
    category = models.ForeignKey(GoodsCategory, on_delete=models.CASCADE, verbose_name="商品类目")

    class Meta:
        verbose_name = '商品信息'
        verbose_name_plural = verbose_name
        ordering = ['id']

    def __str__(self):
        return self.name


class GoodsImage(models.Model):
    """商品轮播图"""
    # 图片对象
    image = models.ImageField(upload_to="goods/images", verbose_name="图片", null=True, blank=True)
    add_time = models.DateTimeField("添加时间", default=datetime.utcnow)
    # 商品：商品轮播图: 1:N
    goods = models.ForeignKey(Goods, on_delete=models.CASCADE, verbose_name="商品", related_name="images")

    class Meta:
        verbose_name = '商品轮播'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.goods.name


class Banner(models.Model):
    """
    首页轮播的商品: 首页的商品轮播图片是大图，跟商品详情里面的图片不一样，所以要单独写一个首页轮播图model
    """
    image = models.ImageField(upload_to='banner', verbose_name="轮播图片")
    index = models.IntegerField("轮播顺序", default=0)  # 1 2 3
    add_time = models.DateTimeField("添加时间", default=datetime.utcnow)

    # 商品：轮播商品
    # goods = models.ForeignKey(Goods, on_delete=models.CASCADE, verbose_name="商品")

    class Meta:
        verbose_name = '首页轮播'
        verbose_name_plural = verbose_name

    def __str__(self):
        # return self.goods.name
        return self.image.name


class HotSearchWords(models.Model):
    """
    搜索栏下方热搜词
    """
    keywords = models.CharField("热搜词", default="", max_length=20)
    index = models.IntegerField("排序", default=0)
    add_time = models.DateTimeField("添加时间", default=datetime.utcnow)

    class Meta:
        verbose_name = '热搜排行'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.keywords


class GoodsCategoryBrand(models.Model):
    """
    某一大类下的宣传商标
    """

    name = models.CharField("品牌名", default="", max_length=30, help_text="品牌名")
    desc = models.TextField("品牌描述", default="", max_length=200, help_text="品牌描述")
    image = models.ImageField(max_length=200, upload_to="brands/")
    add_time = models.DateTimeField("添加时间", default=datetime.utcnow)

    # category = models.ForeignKey(GoodsCategory, on_delete=models.CASCADE, related_name='brands', null=True, blank=True,
    # verbose_name = "商品类目")

    class Meta:
        verbose_name = "宣传品牌"
        verbose_name_plural = verbose_name
        # 重新设置数据库表的名称。
        db_table = "goods_goodsbrand"

    def __str__(self):
        return self.name


class IndexAd(models.Model):
    """
    商品广告
    """
    # 商品广告: 分类: 1:N
    category = models.ForeignKey(GoodsCategory, on_delete=models.CASCADE, related_name='category', verbose_name="商品类目")
    # 商品广告: 商品: 1:N
    goods = models.ForeignKey(Goods, on_delete=models.CASCADE, related_name='goods')

    class Meta:
        verbose_name = '首页广告'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.goods.name
